package com.kangyi.admin.controller;

import com.kangyi.admin.service.FeedbackService;
import com.kangyi.dto.FeedbackPageQueryDTO;
import com.kangyi.dto.ReplyFeedbackDTO;
import com.kangyi.result.FeedbackPageResult;
import com.kangyi.result.Result;
import com.kangyi.vo.FeedbackDetailVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author Blank
 * @version 1.0
 */
@RestController
@RequestMapping("/admin/feedback")
@Slf4j
@Api(tags = "超级管理员-反馈处理相关接口")
public class FeedbackController {

    @Autowired
    private FeedbackService feedbackService;

    @GetMapping("/page")
    @ApiOperation("反馈处理分页查询")
    public Result<FeedbackPageResult> pageQuery(FeedbackPageQueryDTO feedbackPageQueryDTO){
        log.info("反馈处理分页查询:{}", feedbackPageQueryDTO);
        try {
            FeedbackPageResult feedbackPageResult = feedbackService.pageQuery(feedbackPageQueryDTO);
            return Result.success(feedbackPageResult,"查询成功！");
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    @DeleteMapping("/delete")
    @ApiOperation("批量删除反馈")
    @CacheEvict(cacheNames = "feedback_", allEntries = true)
    public Result deleteFeedback(@RequestParam List<Integer> feedbackIds){
        log.info("批量删除反馈:{}", feedbackIds);
        try {
            feedbackService.deleteFeedback(feedbackIds);
            return Result.success("删除成功！");
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    @GetMapping("/detail/{feedbackId}")
    @ApiOperation("查询反馈详情")
//    @Cacheable(cacheNames = "feedback_", key = "'adminDetail_'+#feedbackId")
    public Result<FeedbackDetailVO> detailFeedback(@PathVariable Integer feedbackId){
        log.info("查询反馈详情:{}", feedbackId);
        try {
            FeedbackDetailVO feedbackDetailVO =  feedbackService.detailFeedback(feedbackId);
            return Result.success(feedbackDetailVO,"查询成功！");
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    @PutMapping("/reply")
    @ApiOperation("反馈回复")
    @CacheEvict(cacheNames = "feedback_", allEntries = true)
    public Result replyFeedback(@RequestBody ReplyFeedbackDTO replyFeedbackDTO){
        log.info("反馈回复:{}",replyFeedbackDTO);
        try {
            feedbackService.replyFeedback(replyFeedbackDTO);
            return Result.success("回复成功！");
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

}
